package com.rop.znyx.product.controller;


import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.product.Attr;
import com.rop.znyx.product.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-06-24
 */

@Api(tags = "平台属性管理")
@RestController
@RequestMapping("admin/product/attr")
@RequiredArgsConstructor

public class AttrController {

    private final AttrService attrService;

    /**
     * 获取平台属性列表
     */
    @ApiOperation(value = "获取平台属性列表")
    @GetMapping("{attrId}")
    public Result<List<Attr>> list(@ApiParam(name = "attrId", value = "平台属性分组id", required = true) @PathVariable("attrId") Long attrId) {
        List<Attr> attrs = attrService.getByAttrGroupId(attrId);
        return Result.ok(attrs);
    }

    /**
     * 根据id获取平台属性分组信息
     */
    @ApiOperation("根据id获取平台属性分组信息")
    @GetMapping("get/{id}")
    public Result<Attr> get(@PathVariable("id") Long id) {
        Attr attr = attrService.getById(id);
        return Result.ok(attr);
    }

    /**
     * 新增平台属性分组
     */
    @ApiOperation(value = "新增平台属性分组")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Attr attr) {
        boolean isSuccess = attrService.save(attr);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改平台属性分组
     */
    @ApiOperation(value = "修改平台属性分组")
    @PutMapping("update")
    public Result<Boolean> updateById(@RequestBody Attr attr) {
        boolean isSuccess = attrService.updateById(attr);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 删除平台属性分组
     */
    @ApiOperation(value = "删除平台属性分组")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        boolean isSuccess = attrService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据id列表删除平台属性分组
     */
    @ApiOperation(value = "根据id列表删除平台属性分组")
    @DeleteMapping("batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = attrService.removeByIds(idList);
        return isSuccess ? Result.ok() : Result.fail();
    }
}

